package cn.iocoder.yudao.module.yyzx.service.cfood;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.yyzx.controller.admin.cfood.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.cfood.CFoodDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.yyzx.dal.mysql.cfood.CFoodMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.yyzx.enums.ErrorCodeConstants.*;

/**
 * 客户膳食信息 Service 实现类
 *
 * @author jzh
 */
@Service
@Validated
public class CFoodServiceImpl implements CFoodService {

    @Resource
    private CFoodMapper cFoodMapper;

    @Override
    public Integer createCFood(CFoodSaveReqVO createReqVO) {
        // 插入
        CFoodDO cFood = BeanUtils.toBean(createReqVO, CFoodDO.class);
        cFoodMapper.insert(cFood);
        // 返回
        return cFood.getId();
    }

    @Override
    public void updateCFood(CFoodSaveReqVO updateReqVO) {
        // 校验存在
        validateCFoodExists(updateReqVO.getId());
        // 更新
        CFoodDO updateObj = BeanUtils.toBean(updateReqVO, CFoodDO.class);
        cFoodMapper.updateById(updateObj);
    }

    @Override
    public void deleteCFood(Integer id) {
        // 校验存在
        validateCFoodExists(id);
        // 删除
        cFoodMapper.deleteById(id);
    }

    private void validateCFoodExists(Integer id) {
        if (cFoodMapper.selectById(id) == null) {
            throw exception(C_FOOD_NOT_EXISTS);
        }
    }

    @Override
    public CFoodDO getCFood(Integer id) {
        return cFoodMapper.selectById(id);
    }

    @Override
    public PageResult<CFoodDO> getCFoodPage(CFoodPageReqVO pageReqVO) {
        return cFoodMapper.selectPage(pageReqVO);
    }

}